#!usr/bin/python
#coding=utf-8

import re
import urllib.request
import urllib.error
import socket
#import ltmex

from ltmex.ltmcode import decodeHtml
from ltmex.ltmcode import decodeHtmlByUrl



#伪装浏览器的默认header
defaultHeader = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '  
                        'Chrome/51.0.2704.63 Safari/537.36'
    }

#页面内网络图片链接 (,)
# ((?i)jpg|png|jpeg|bmp|gif) 忽略后缀名大小写
# \s 匹配空格，制表符，换行符
patHttpImage = r'src="(https*://[^"^\s]+?\.((?i)jpg|png|jpeg|bmp|gif))"'

#页面内网络视频链接 (,)
patHttpVideo = r'src="(https*://[^"^\s]+?\.((?i)MP4|avi|mpeg|rmvb|rm|wmv|mpg))"'

#页面内文本链接(,)
patHttpDoc = r'src="(https*://[^"^\s]+?\.((?i)txt|pdf|doc|docx))"'

#页面内种子文件网络链接 (,)
patHttpTorrent = r'src="(https*://[^"^\s]+?\.((?i)torrent))"'

#页面内磁力链接 (,)
patMagnet = r'(magnet:\?xt=urn:btih:[^<^>^"^\s^,^.]+?)[<|>|"]'




#pattern为正则表达式字符串
def grab( pattern, data ):
#    print( data)
#    print(pattern)
#    newdata = decodeHtml(data)

    if data and pattern:
        c = re.compile(pattern)
        li = re.findall(c,data)
        return li
    else:
        return {}


def grabHtml(pattern, url, params={}, headers={}):

    if params:
        #post
        print('post')

    else:
        #get
        print('get')

    print (url)
    res = decodeHtmlByUrl(url=url, params=params, header = headers)
    li  = grab(pattern, res)
        
    
    return li



    
